/*
 * Created on 2005-3-31
 * Copyright: Bluejay Networks Inc. 2004
 *
 * This software is the proprietary information of Bluejay Networks Inc. & 
 * Inquiry Management Systems Ltd.  
 * Use is subject to license terms.
 * 
 */
package com.ims.iaa;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Properties;

import com.ims.iaa.AD.HrefParser;

/**
 * @author changsheng gong
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ModifyUrl {
    public static void main(String[] args) {

        Properties pro = new Properties();

		try  {
			pro.load(new FileInputStream("security\\db.properties"));
		} catch (IOException e)  {
			IaaUtil.log("Can not open the database properties file,IOException:" + e);
		}
		
		String user = pro.getProperty("db.userName");
		String password = pro.getProperty("db.password");
		String driver = pro.getProperty("db.driverName");
		String url = pro.getProperty("db.url");

		try {
            Class.forName(driver);     
            Connection cn = DriverManager.getConnection( url, user, password);
        
            Statement stmt = null;
            ResultSet rs = null;
		
            stmt = cn.createStatement();
            String sql =   "SELECT dbo.tblImpression.strImageURL, dbo.tblImage.strImageType ,dbo.tblImage.intImageID, dbo.tblImpression.intImpressionID"
                		 + " FROM dbo.tblImpression INNER JOIN "
                		 + " dbo.tblImage ON dbo.tblImpression.intImageID = dbo.tblImage.intImageID"
                         + " WHERE (dbo.tblImage.strImageType NOT LIKE 'image%') AND "
                         + "(dbo.tblImage.strImageType NOT LIKE 'application%') AND (dbo.tblImpression.bitState = 1)" ;
            
            rs = stmt.executeQuery( sql );
            
            cn.setAutoCommit(false);
            
            System.out.println("**********************************************");
            System.out.println("Analyze url begin:");
            System.out.println("**********************************************");
            
            HashMap ht = new HashMap();
            int count = 0;
            while(rs.next()){
                try{
                    int imageID = rs.getInt(3);
                    Integer imageId = new Integer(imageID);
                    if(ht.containsKey(imageId) == false){
                        ht.put(imageId , imageId);
                    
		                URL oldStrImageUrl = URLUtil.getURL(rs.getString(1));
		                System.out.println( "\tstart get redirect url of " + oldStrImageUrl);
		                Statement st = null ;
	                    ResultSet r = null;
	                    
		                if( oldStrImageUrl != null){
		                    HrefParser hrefParser = new HrefParser( oldStrImageUrl, null);
		                    if(hrefParser.connect() == false ){
		                        System.out.println("\t\tconnect failed");	                      
		                    }else{
			                    String newStrImageUrl = hrefParser.getRedirectURL().toString();
			                    String newStrImageType = hrefParser.getMimeType();
			                    int iImageId = rs.getInt(3);
			                    int iImpressionId = rs.getInt(4);
			                    System.out.println("\t\tthe redirect url is " + newStrImageUrl);
			                    
			                    
			                    String sq = "UPDATE dbo.tblImpression " +
			                    		" SET dbo.tblImpression.strImageURL = '" + newStrImageUrl +"'"+
			                            " WHERE dbo.tblImpression.intImpressionID = " + iImpressionId ;
			                    st = cn.createStatement();
			                    st.executeUpdate(sq);
			                
		
			                    sq = "UPDATE dbo.tblImage " +
			                    	 "SET dbo.tblImage.strImageType = '" + newStrImageType +"'"+
			                    	 "WHERE dbo.tblImage.intImageID = " + iImageId;
			                    st = cn.createStatement();
		                        st.executeUpdate(sq);
		
		                        cn.commit();
		                        System.out.println("\t\tset the  strImageType to " + newStrImageType);
		                        System.out.println("\tend analyze an image");
			                    st.close();
			                    
			                    count++;
		                    }//if-else
		                }//if
	                } //if 
	             }catch(Exception e){
	                 cn.rollback();	              
	             }
	             
            }//while            
            System.out.println("**********************************************");
            System.out.println("Analyze url end: have analyze " + count + " Urls");
            System.out.println("**********************************************\n\n");
		} catch (Exception ex) {
      
        } 
    }
}
